package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.SerialNoHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.wrap.EnableApiCallBack;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.DataUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.ApiCallBackEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.formdata.pojo.SaasSpecialParamPojo;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.dao.PaymentSheetDao;
import com.xbongbong.saas.domain.entity.PaymentSheetEntity;
import com.xbongbong.saas.domain.entity.ext.PaymentSheetEntityExt;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.SheetAloneEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.help.DefaultAttrHelp;
import com.xbongbong.saas.help.ShowFormatHelp;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import com.xbongbong.saas.model.PaymentSheetModel;
import com.xbongbong.saas.model.PaymentSheetSubDataModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import org.elasticsearch.action.search.SearchRequest;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/25 15:28
 * @since v1.0
 */
@Service("paymentSheetModel")
public class PaymentSheetModelImpl implements PaymentSheetModel {
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private PaymentSheetDao paymentSheetDao;
    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;
    @Resource
    private PaymentSheetSubDataModel paymentSheetSubDataModel;
    @Resource
    private DefaultAttrHelp defaultAttrHelp;
    @Resource
    private SerialNoHelp serialNoHelp;
    @Resource
    private ShowFormatHelp showFormatHelp;
    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Resource
    private UserModel userModel;

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.NEW)
    public Integer insert(PaymentSheetEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        StandardizeDataUtil.standardizeNotString(entity);
        Integer insert = paymentSheetDao.insert(entity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
        paasEsModel.insert(insertDTO);
        paasFormSubDataModel.insertSubFormData(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        return insert;
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.NEW, isBatch = true)
    public List<PaymentSheetEntityExt> insertBatch(List<PaymentSheetEntityExt> list) throws XbbException{
        // TODO: 2020/9/22
        return insertBatch(list, null);
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.NEW, isBatch = true)
    public List<PaymentSheetEntityExt> insertBatch(List<PaymentSheetEntityExt> list, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException{
        for (PaymentSheetEntityExt entity : list) {
            paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        }
        StandardizeDataUtil.standardize4EntityList(list);
        paymentSheetDao.insertBatch(list);
        //ES数据插入
        List<InsertDTO> insertList = new ArrayList<>();
        for (PaymentSheetEntityExt item : list) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(item.getCorpid() + "_" + item.getId());
            item.setDataId(item.getId());
            PaymentSheetEntity paasFormDataEntity = new PaymentSheetEntity();
            BeanUtil.copyProperties(item, paasFormDataEntity);
            insertDTO.setSource(BeanUtil.convertBean2Map(paasFormDataEntity, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
            insertList.add(insertDTO);
        }
        if(refreshPolicy == null){
            paasEsModel.insertBatch(insertList);
        }else{
            paasEsModel.insertBatch(insertList, refreshPolicy);
        }

        paasFormSubDataModel.insertBatchSubFormData(list, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        return list;
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.EDIT)
    public Integer update(PaymentSheetEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        StandardizeDataUtil.standardizeNotString(entity);
        Integer integer = paymentSheetDao.update(entity);
        Integer updateSub = paasFormSubDataModel.updateSubFormData(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        //ES的更新
        if (Objects.equals(1, integer) || Objects.equals(1, updateSub)) {
            integer = paymentSheetDao.updateTime(now + 1, entity.getId(), entity.getCorpid());
            InsertDTO insertDTO = paasEsModel.getSaveDTO(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
            paasEsModel.update(insertDTO);

        }
        if (Objects.nonNull(entity.getSubFormData()) && !entity.getSubFormData().isEmpty()) {
            entity.getData().putAll(entity.getSubFormData());
        }
        return integer;
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.EDIT, isBatch = true)
    public Integer updateBatch(List<UpdateDataEntity> list, String corpid) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(list);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(list);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(list)) {
            StandardizeDataUtil.standardizeList(list);
            integer = paymentSheetDao.updateBatch(list, corpid);
            paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
        }
        // todo
        if (!subList.isEmpty()) {
            paymentSheetSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.EDIT, isBatch = true)
    public Integer updateBatch(List<UpdateDataEntity> list, String corpid, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(list);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(list);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(list)) {
            StandardizeDataUtil.standardizeList(list);
            integer = paymentSheetDao.updateBatch(list, corpid);
            paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET, refreshPolicy);
        }
        // todo
        if (!subList.isEmpty()) {
            paymentSheetSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.EDIT, isBatch = true)
    public Integer updateBatch(List<UpdateDataEntity> list, String corpid, boolean needThrow) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(list);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(list);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(list)) {
            StandardizeDataUtil.standardizeList(list);
            integer = paymentSheetDao.updateBatch(list, corpid);
            paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET, needThrow);
        }
        // todo
        if (!subList.isEmpty()) {
            paymentSheetSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.EDIT, isBatch = true)
    public Integer updateFlowStatusBatch(List<UpdateDataEntity> list, String corpid) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(list);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(list);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(list)) {
            integer = paymentSheetDao.updateFlowStatusBatch(list, corpid);
            paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
        }
        // todo
        if (!subList.isEmpty()) {
            paymentSheetSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }

    @Override
    public List<PaymentSheetEntityExt> findEntitys(Map<String, Object> param) {
        List<PaymentSheetEntityExt> entitys = paymentSheetDao.findEntitys(param);
        Object corpid = param.get(ParameterConstant.CORPID);
        if (Objects.nonNull(corpid)) {
            paasFormSubDataModel.findSubEntity(corpid.toString(), entitys, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        }
        return entitys;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return paymentSheetDao.getEntitysCount(param);
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.NEW)
    public Integer save(PaymentSheetEntityExt paymentEntity) throws XbbException {
        if (paymentEntity.getId() == null || paymentEntity.getId() == 0L) {
            return insert(paymentEntity);
        }  else {
            return update(paymentEntity);
        }
    }

    @Override
    public PaymentSheetEntityExt getByKey(Long key, String corpid) {
        PaymentSheetEntityExt entityExt = paymentSheetDao.getByKey(key, corpid);
        paasFormSubDataModel.findSubEntity(corpid, Collections.singletonList(entityExt), IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        return entityExt;
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.DELETE, isBatch = true)
    public Integer deleteBatch(List<Long> list, String corpid, Integer del) throws XbbException {
        Integer integer = paymentSheetDao.deleteBatchAppointDel(list, corpid, del);
        //ES根据id批量设置del
        paasEsModel.deleteBatch(list, corpid, del, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
        paasFormSubDataModel.deleteBatchByDataId(list, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB, DelEnum.DELETE_SUBFORM);
        return integer;
    }

    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.DELETE)
    public Integer deleteByKey(Long key,String corpid) throws XbbException{
        return deleteByKeyAppointDel(key, corpid, DelEnum.DELETE);
    }

    @Override
    public Integer deleteByKeyAppointDel(Long id, String corpid, DelEnum delEnum) throws XbbException {
        DeleteDTO deleteDTO = new DeleteDTO();
        deleteDTO.setEsId(corpid + "_" + id);
        deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
        paasEsModel.delete(deleteDTO, delEnum);
        Integer integer = paymentSheetDao.deleteByKeyAppointDel(id, corpid, delEnum.getDel());
        paasFormSubDataModel.deleteByDataId(id, corpid, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        return integer;
    }

    @Override
    public Integer updateBatchTemp(List<UpdateDataEntity> list) throws XbbException {
        StandardizeDataUtil.standardizeList(list);
        Integer integer = paymentSheetDao.updateBatchTemp(list);
        paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
        // TODO 慎用的不敢写
        return integer;
    }
    
    /**
     * @param list   更新的数据
     * @param corpid 公司id
     * @return
     * @author chenshan
     * @date 2019/11/28 21:27
     */
    @Override
    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.EDIT, isBatch = true)
    public Integer updateBatchForData(List<? extends PaasFormDataEntity> list, String corpid) throws XbbException {
        // TODO: 2020/9/22
        return updateBatchForData(list, corpid, true);
    }

    @EnableApiCallBack(callBackBusiness = ApiCallBackEnum.PAYMENT_SHEET, operateTypeEnum = OperateTypeEnum.EDIT, isBatch = true)
    public Integer updateBatchForData(List<? extends PaasFormDataEntity> list, String corpid, boolean needThrow) throws XbbException {
        if(CollectionsUtil.isEmpty(list)){
            return 0;
        }
        for (PaasFormDataEntity paasFormDataEntityExt : list) {
            paasFormSubDataModel.transferSubForm(paasFormDataEntityExt, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        }
        StandardizeDataUtil.standardize4EntityList(list);
        Integer updateCount = paymentSheetDao.updateBatchForAll(list,corpid);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : list) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET));
        }
//        List<InsertDTO> insertDTOList = new ArrayList<>();
//        for ( PaymentSheetEntityExt entityExt : list){
//            InsertDTO insertDTO = new InsertDTO();
//            insertDTO.setEsId(corpid+"_"+entityExt.getId());
//            PaymentSheetEntityExt paymentSheetEntityExt = new PaymentSheetEntityExt();
//            BeanUtil.copyProperties(entityExt, paymentSheetEntityExt);
//            paymentSheetEntityExt.setDataId(entityExt.getId());
//            insertDTO.setSource(BeanUtil.convertBean2Map(paymentSheetEntityExt));
//            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
//            insertDTOList.add(insertDTO);
//        }
        paasEsModel.updateBatch(insertDTOList,WriteRequest.RefreshPolicy.WAIT_UNTIL,needThrow);
        // TODO
        return updateCount;
    }

    @Override
    public List<PaymentSheetEntityExt> findAffectBalanceEntitys(Map<String, Object> param) {
        //只要父 和  类型只要2，3，6，8
        param.put(BasicConstant.ALONE_IN, Arrays.asList(SheetAloneEnum.NORMAL.getCode(), SheetAloneEnum.PARENT.getCode()));
        List<PaymentSheetEntityExt> entityExts = paymentSheetDao.findAffectBalanceEntitys(param);
        Object corpid = param.get(ParameterConstant.CORPID);
        if (Objects.nonNull(corpid)) {
            paasFormSubDataModel.findSubEntity(corpid.toString(), entityExts, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        }
        return entityExts;
    }
    
    /**
     * 获取坏账回款单
     *
     * @param param
     * @return
     */
    @Override
    public List<PaymentSheetEntityExt> getBadPaymentSheet(Map<String, Object> param) {
        //只要父 和子  类型只要7
        param.put(BasicConstant.ALONE_IN, Arrays.asList(SheetAloneEnum.NORMAL.getCode(), SheetAloneEnum.CHILD.getCode()));
        param.put("flowStatusIn",Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(),FlowStatusEnum.PASS.getType()));
        List<PaymentSheetEntityExt> entityExts = paymentSheetDao.getBadPaymmentSheet(param);
        Object corpid = param.get(ParameterConstant.CORPID);
        if (Objects.nonNull(corpid)) {
            paasFormSubDataModel.findSubEntity(corpid.toString(), entityExts, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        }
        return entityExts;
    }
    
    /**
     * 得到坏账条数
     *
     * @param param
     * @return
     */
    @Override
    public Integer getBadPaymentSheetCount(Map<String, Object> param) {
        //只要父 和子  类型只要7
        param.put(BasicConstant.ALONE_IN, Arrays.asList(SheetAloneEnum.NORMAL.getCode(), SheetAloneEnum.CHILD.getCode()));
        param.put("flowStatusIn",Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(),FlowStatusEnum.PASS.getType()));
        return paymentSheetDao.getBadPaymmentSheetCount(param);
    }

    @Override
    public List<PaymentSheetEntityExt> getPaymentSheetByCorpidScript(Map<String, Object> param) {
        List<PaymentSheetEntityExt> entityExts = paymentSheetDao.getPaymentSheetByCorpidScript(param);
        return entityExts;
    }

    @Override
    public List<PaymentSheetEntityExt> getPaymentSheetByIdIn(String corpid,List<Long> idIn){
        List<PaymentSheetEntityExt> entityExts = paymentSheetDao.getPaymentSheetByIdIn(corpid, idIn);
        paasFormSubDataModel.findSubEntity(corpid, entityExts, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        return entityExts;
    }

    @Override
    public List<PaymentSheetEntityExt> getParentAndSubPaymentSheet(String corpid,List<String> uuidIn){
        List<PaymentSheetEntityExt> entityExts = paymentSheetDao.getParentAndSubPaymentSheet(corpid, uuidIn);
        paasFormSubDataModel.findSubEntity(corpid, entityExts, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_SUB);
        return entityExts;
    }

    @Override
    public List<PaymentSheetEntityExt> findEntitysWithoutSub(Map<String, Object> param) {
        return paymentSheetDao.findEntitys(param);
    }

    @Override
    public Long getMaxId(Map<String, Object> param) {
        return paymentSheetDao.getMaxId(param);
    }

    @Override
    public void formatUpdateExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        //红冲回款单时 编号处理
        SaasSpecialParamPojo saasSpecialParamPojo = formatExplainDTO.getSaasSpecialParamPojo();
        if (saasSpecialParamPojo != null && Objects.equals(saasSpecialParamPojo.getFromRed(), BasicConstant.ONE)) {
            List<FieldAttrEntity> explainList = formatExplainDTO.getExplainList();
            String serialNo = "";
            for (FieldAttrEntity fieldAttrEntity : explainList) {
                serialNo = serialNoHelp.getSerialNo(serialNo, formatExplainDTO.getCorpid(), formatExplainDTO.getFormId(), fieldAttrEntity);
            }
            PaasFormDataEntityExt dataEntity = formatExplainDTO.getDataEntity();
            Long id = dataEntity.getId();
            String sheetNo = dataEntity.getSerialNo();
            dataEntity.setSerialNo(serialNo);
            dataEntity.getData().put(PaymentSheetEnum.RED_SHEET.getAttr(), defaultAttrHelp.packegeLinkBusinessJsonArray(id, sheetNo));
        }
    }

    @Override
    public void formatData(SaasAnalysisDataDTO saasAnalysisDataDTO, JSONObject result) throws XbbException {
        showFormatHelp.formatLinkBusiness4Show(result, PaymentSheetEnum.CONTRACT.getAttr(), PaymentSheetEnum.CONTRACT_LINK_TEXT.getAttr(), PaymentSheetEnum.CONTRACT.getFieldType());
        showFormatHelp.formatLinkBusiness4ShowBySource(result, PaymentSheetEnum.CUSTOMER_ID.getAttr(), PaymentSheetEnum.CUSTOMER_NAME_LINK_TEXT.getAttr(), PaymentSheetEnum.CUSTOMER_ID.getFieldType(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), XbbRefTypeEnum.CONTRACT.getCode(), saasAnalysisDataDTO.getCorpid());
        showFormatHelp.formatLinkBusiness4Show(result, PaymentSheetEnum.PAYMENT.getAttr(), PaymentSheetEnum.PAYMENT_LINK_TEXT.getAttr(), PaymentSheetEnum.PAYMENT.getFieldType());
        showFormatHelp.formatLinkBusiness4Show(result, PaymentSheetEnum.RED_SHEET.getAttr(), PaymentSheetEnum.RED_SHEET_LINK_TEXT.getAttr(), PaymentSheetEnum.RED_SHEET.getFieldType());
        showFormatHelp.formatLinkBusiness4Show(result, PaymentSheetEnum.PAYMENT_TASK.getAttr(), PaymentSheetEnum.PAYMENT_TASK_LINK_TEXT.getAttr(), PaymentSheetEnum.PAYMENT_TASK.getFieldType());
    }

    @Override
    public List<PaasFormDataEntityExt> getPaymentSheetEsDataListByCustomerIds(String corpid, List<Long> customerIds, List<String> typeList) throws XbbException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termsQuery("data." + PaymentSheetEnum.CUSTOMER_ID.getAttr(), customerIds));
        boolQueryBuilder.filter(termQuery("del", 0));
        boolQueryBuilder.filter(termsQuery("flowStatus", Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
        boolQueryBuilder.filter(termsQuery("data."+PaymentSheetEnum.PAYMENT_SHEET_TYPE.getAttr(), typeList));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET.getType()));
        sourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET.getIndex());
        PageRequest pageRequest = EsUtil.setPage(sourceBuilder, 0, PaasConstant.ES_MAX_PAGE_SIZE);
        searchRequest.source(sourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, searchRequest, PaasFormDataEntityExt.class);
        return esEntities.getContent();
    }

    @Override
    public List<PaasFormDataEntityExt> getPaymentSheetEsDataListByCustomerId(String corpid, Long dataId, List<String> codeList, UserVO loginUser) throws XbbException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery("data." + PaymentSheetEnum.CUSTOMER_ID.getAttr(), dataId));
        boolQueryBuilder.filter(termQuery("del", 0));
        boolQueryBuilder.filter(termsQuery("flowStatus", Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET.getType()));
        if (!CollectionsUtil.isEmpty(codeList)) {
            boolQueryBuilder.filter(termsQuery("data." + PaymentSheetEnum.PAYMENT_SHEET_TYPE.getAttr(), codeList));
        }
        boolQueryBuilder.filter(termsQuery(BasicConstant.ALONE, Arrays.asList(1, 2)));
        UserEntity userEntity = new UserEntity();
        BeanUtil.copyProperties(loginUser, userEntity);
        sourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET.getIndex());
        PageRequest pageRequest = EsUtil.setPage(sourceBuilder, 0, PaasConstant.ES_MAX_PAGE_SIZE);
        searchRequest.source(sourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, searchRequest, PaasFormDataEntityExt.class);
        return esEntities.getContent();
    }
    /**
     * 更新回款单时间
     *
     * @param id
     * @param corpid
     */
    @Override
    public void updateTime(Long id, String corpid) throws XbbException {
        Long updateTime = DateTimeUtil.getInt();
        paymentSheetDao.updateTime(updateTime,id, corpid);
        InsertDTO insertDTO = new InsertDTO();
        insertDTO.setEsId(corpid+"_"+id);
        Map<String, Object> map = new HashMap<>();
        map.put(FieldTypeEnum.UPDATETIME.getAlias(), updateTime);
        insertDTO.setSource(map);
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET);
        paasEsModel.update(insertDTO);
    }

}
